package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.CheckBean;
import com.project.bean.CheckInfoBean;
import com.project.bean.ClientBean;
import com.project.bean.UserBean;
import com.project.dao.ICheckDao;
import com.project.service.ICheckService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class CheckServiceImpl implements ICheckService {
    @Autowired
    private ICheckDao iCheckDao;

    @Override
    public InfoResult addNewCheck(int clientId, int userId, String address) {
        CheckBean checkBean = new CheckBean();
        ClientBean cl = new ClientBean();
        cl.setClientId(clientId);
        UserBean us = new UserBean();
        us.setUserId(userId);
        checkBean.setUserBean(us);
        checkBean.setClientBean(cl);
        checkBean.setAddress(address);
        checkBean.setSaleDate(LocalDate.now());
        int x = iCheckDao.addNewCheck(checkBean);
        if (x == 1) {
            return new InfoResult(0, "添加新订单成功");
        }
        return new InfoResult(1, "添加新订单失败");
    }

    @Override
    public CheckBean findCheckById(int checkId) {
        CheckBean che = iCheckDao.findCheckById(checkId);
        List<CheckInfoBean> list = che.getCheckInfoList();
        int x = 0;
        for (CheckInfoBean c : list) {
            x += c.getCheckOutNum() * c.getEndSalePrice();
        }
        che.setAllPrice(x);
        return che;
    }

    @Override
    public PageInfo<CheckBean> findCheckByPage(int pageNum,
                                               String clientName,
                                               LocalDate startDate,
                                               LocalDate endDate) {
        PageHelper.startPage(pageNum, 3);
        PageInfo<CheckBean> info = PageInfo.of(iCheckDao.findCheckByPage(clientName, startDate, endDate));
        return info;
    }
}
